// pages/pay/pay.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active:0,
  },
  onactive(e){
    var inx =e.currentTarget.dataset.index;
    this.setData({active:inx,total:this.data.list[inx].price})
  },
  gopay(){
    var params={
      price:this.data.total,
      openid:this.data.user.openid
    }
    this.Api.gopay(params).then(res=>{
      wx.requestPayment({
        timeStamp: res.data.timeStamp,
        nonceStr: res.data.nonceStr,
        package: res.data.package,
        signType: res.data.signType,
        paySign: res.data.paySign,
        success: (obj)=> {
          if (obj.errMsg == "requestPayment:ok") {
            var rep = {
              uid: this.data.user.id,
              coin: this.data.list[this.data.active].num
            }
            this.Api.gopayCallBack(rep).then(data => {
              this.Global.showMsg(res.msg).then(()=>{
                wx.navigateBack({
                  delta: 1,
                })
              })
            })

          }
        },
        fail: function(res) {
          console.log(res)
        },
        complete: function(res) {},
      })
      
    })  
  },
  init(){
    var uid = this.data.user.id;
    this.Api.getmyPayList({ uid }).then(res => {
      this.setData({
        txt:res.data.txt,
        list:res.data.list,
        total:res.data.list[0].price
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.Global = app.Global;
    this.Api = app.Global.Api;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.Global.getWxUser().then((obj) => {
      console.log(obj)
      this.setData({
        user:obj
      }, () => {
        this.init()
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

 
})